/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewColumnDialog.java
 *
 * Created on May 22, 2010, 9:28:03 PM
 */

package org.hbaseexplorer.components;

import java.io.IOException;
import javax.swing.DefaultComboBoxModel;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.hbaseexplorer.domain.HBTriplet;
import org.hbaseexplorer.domain.Table;
import org.hbaseexplorer.exception.ExplorerException;
import org.jdesktop.application.Action;

/**
 *
 * @author zaharije
 */
public class NewColumnDialog extends javax.swing.JDialog {

    private Table table;
    private HBTriplet triplet;

    /** Creates new form NewColumnDialog */
    public NewColumnDialog(java.awt.Frame parent, Table table) {
        super(parent, true);
        this.table = table;
        this.triplet = null;
        initComponents();
        fillData();
    }

    private void fillData() {
        try {
            HColumnDescriptor[] descriptors = table.getHTable().getTableDescriptor().getColumnFamilies();
            DefaultComboBoxModel model = new DefaultComboBoxModel();
            for(HColumnDescriptor desc : descriptors) {
                model.addElement(desc.getNameAsString());
            }
            familyComboBox.setModel(model);
        } catch (IOException ex) {
            throw new ExplorerException("Error getting column families.");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        familyComboBox = new javax.swing.JComboBox();
        columnTextField = new javax.swing.JTextField();
        valueTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.hbaseexplorer.HBaseExplorerApp.class).getContext().getResourceMap(NewColumnDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setModal(true);
        setName("Form"); // NOI18N
        setResizable(false);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        familyComboBox.setName("familyComboBox"); // NOI18N

        columnTextField.setText(resourceMap.getString("columnTextField.text")); // NOI18N
        columnTextField.setName("columnTextField"); // NOI18N

        valueTextField.setText(resourceMap.getString("valueTextField.text")); // NOI18N
        valueTextField.setName("valueTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(org.hbaseexplorer.HBaseExplorerApp.class).getContext().getActionMap(NewColumnDialog.class, this);
        jButton1.setAction(actionMap.get("btnAddClickAction")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("btnCancelClickAction")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel1)
                            .add(jLabel3))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(columnTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                            .add(valueTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                            .add(familyComboBox, 0, 248, Short.MAX_VALUE))
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jButton1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton2)
                        .add(68, 68, 68))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(familyComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(columnTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(valueTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                    .add(jButton2)
                    .add(jButton1))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void btnAddClickAction() {
        String family = familyComboBox.getModel().getSelectedItem().toString();
        String column = columnTextField.getText();
        String value = valueTextField.getText();

        triplet = new HBTriplet(family.getBytes(), column.getBytes(), value.getBytes());
        dispose();
    }

    @Action
    public void btnCancelClickAction() {
        dispose();
    }

    public HBTriplet getTriplet() {
        return triplet;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField columnTextField;
    private javax.swing.JComboBox familyComboBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField valueTextField;
    // End of variables declaration//GEN-END:variables

}
